-- 为了应付考试的特殊活动 元宝充值反元宝
local Config = require("Config")
local Lang = require("common.Lang")
local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local ItemDefine = require("bag.ItemDefine")
local Grid = require("bag.Grid")
local GridLogic = require("bag.GridLogic")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local RoleUIControl = require("role.RoleUIControl")

local ACT_START_TIME = {2015, 9, 16}
local ACT_END_TIME = {2015, 9, 16}

local MAX_GIFT_CNT = 10 -- 最大领取数
local CHONGZHI_VALUE = 500 --每充值多少可以返利

START_TIME_SEC = START_TIME_SEC or nil
END_TIME_SEC = END_TIME_SEC or nil
GRID_ITEM = GRID_ITEM or nil

function init()
    local tempTable = {}
    tempTable.year, tempTable.month, tempTable.day, tempTable.hour = ACT_START_TIME[1], ACT_START_TIME[2], ACT_START_TIME[3], 0
    START_TIME_SEC = os.time(tempTable)
    tempTable.year, tempTable.month, tempTable.day, tempTable.hour = ACT_END_TIME[1], ACT_END_TIME[2], ACT_END_TIME[3], 24
    END_TIME_SEC =  os.time(tempTable)

    GRID_ITEM = Grid.create(nil, ItemDefine.ITEM_ID_YUANBAO, CHONGZHI_VALUE)
end

local function getCanGetCnt(human)
    local canGetCnt = 0
    if human.db.ChongzhiPresentCnt < MAX_GIFT_CNT then
        canGetCnt = (human.db.dailyChongzhi - human.db.ChongzhiPresentCnt * CHONGZHI_VALUE) / CHONGZHI_VALUE
        canGetCnt = math.floor(canGetCnt)
        if canGetCnt + human.db.ChongzhiPresentCnt > MAX_GIFT_CNT then
            canGetCnt = MAX_GIFT_CNT - human.db.ChongzhiPresentCnt
        end
    end

    return canGetCnt
end

function query(human)
    if isOpen() ~= true then
        RoleUIControl.sendLine1FnIcon(human)
        return
    end

    ObjHuman.updateDaily(human)

    local now = os.time()
    local nDayEndTime = Util.remainDaySeconds() + now
    local nDayStartTime = nDayEndTime - 86400 + 1

    local mm = Msg.gc.GC_CHONGZHI_PRESENT_QUERY
    mm.chongzhi = human.db.dailyChongzhi
    mm.totalGetCnt = MAX_GIFT_CNT
    mm.hasGetCnt = human.db.ChongzhiPresentCnt
    mm.canGetCnt = getCanGetCnt(human)
    if human.db.ChongzhiPresentCnt < MAX_GIFT_CNT and human.db.dailyChongzhi - human.db.ChongzhiPresentCnt * CHONGZHI_VALUE >= CHONGZHI_VALUE then
        mm.canGetStatus = 1
    else
        mm.canGetStatus = 0
    end
    Grid.makeItem(GRID_ITEM, mm.item, 0, human)
    mm.startTime = nDayStartTime
    mm.endTime = nDayEndTime
    Msg.send(mm, human.fd)

end

function get(human)
    if isOpen() ~= true then
        RoleUIControl.sendLine1FnIcon(human)
        return
    end

    ObjHuman.updateDaily(human)

    local canGetCnt = getCanGetCnt(human)

    if canGetCnt <= 0 then return end

    human.db.ChongzhiPresentCnt = human.db.ChongzhiPresentCnt + canGetCnt
    BagLogic.addItem(human, ItemDefine.ITEM_ID_YUANBAO, 1, CHONGZHI_VALUE * canGetCnt, "chongzhipresent")

    local mm = Msg.gc.GC_CHONGZHI_PRESENT_GET
    Msg.send(mm, human.fd)

    query(human)
end

function isOpen()
    if Config.IS_MIDDLE == true then return end

    --服务器判断
    if Config.SVR_INDEX >= 100001 and Config.SVR_INDEX <= 100027 then
        --时间判断
        local nowTime =  os.time()
        if nowTime>=START_TIME_SEC and nowTime<=END_TIME_SEC then
            return true
        end
    end
end

function canShow(human)
    return isOpen()
end
